package warehouse

import (
	"context"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"insighttracking/apps/admin/auth"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetWarehouseListApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetWarehouseListApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetWarehouseListApiLogic {
	hostname, _ := os.Hostname()
	return &GetWarehouseListApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetWarehouseListApiLogic) GetWarehouseListApi() (resp []types.WarehouseListResp, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return make([]types.WarehouseListResp, 0), nil
	}
	warehouseList, err := service.WarehouseService.GetWarehouseListApi(l.ctx, accountInfo.EnterpriseId)
	if err != nil {
		return make([]types.WarehouseListResp, 0), nil
	}
	return k.Map(warehouseList, func(item *model.WarehouseEntity, index int) types.WarehouseListResp {
		return types.WarehouseListResp{
			ID:   item.ID,   // 主键id
			Name: item.Name, // 仓库名称
			Code: item.Code, // 仓库编码
		}
	}), nil
}
